package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.TypeReference;
import com.umeng.message.PushAgent;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.service.UserService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.service.validate.UserValidate;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.MD5;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.ToastUtil;
import reduce.fz.com.reduce.util.base.PreferenceUtil;
import reduce.fz.com.reduce.util.base.ReduceApplication;

public class LoginActivity extends BaseToolBarActivity implements View.OnClickListener {
    //手机号码
    private EditText mLoginName;
    //用户密码
    private EditText mLoginPassword;
    //登录按钮
    private TextView mTvLoginConfirm;
    //忘记密码
    private TextView mForgetPassword;

    private LoadingDialog dialog;

    private UserService userService = ServiceBuilder.builderUserService(null);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setToolbarTitle("登录");
        initWidget();
        initListener();


    }


    @Override
    public void initWidget() {
        mLoginName = (EditText) this.findViewById(R.id.et_login_name);
        mLoginName.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        mLoginPassword = (EditText) this.findViewById(R.id.et_login_password);
        mTvLoginConfirm = (TextView) this.findViewById(R.id.tv_login_confirm);
        mForgetPassword = (TextView) this.findViewById(R.id.forget_password);

    }

    @Override
    public void initListener() {
        mTvLoginConfirm.setOnClickListener(this);
        mForgetPassword.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_password:
                forgetPassword();
                break;
            case R.id.tv_login_confirm:
                String password = MD5.md5(mLoginPassword.getText().toString());
                login(mLoginName.getText().toString(), password);
                break;
            default:
                break;
        }
    }

    /**
     * 验证手机号和密码
     *
     * @param mobile   手机号
     * @param password 密码
     * @return
     */
    public Boolean paramValidate(String mobile, String password) {
        if (!UserValidate.mobileCheck(mobile)) {
            ToastUtil.showMessage(getResources().getString(R.string.mobile_input_error));
            InputMethodUtil.reuestFocus(mLoginName);
            return false;
        }
        //验证密码
        if (!UserValidate.passwordCheck(password)) {
            ToastUtil.showMessage(getResources().getString(R.string.password_input_error));
            InputMethodUtil.reuestFocus(mLoginPassword);
            return false;
        }
        return true;
    }


    public void login(String mobile, String password) {
        User user = new User();
        user.mobile = mobile;
        user.password = password;
        //加载进度框
        //验证手机号
        if (!paramValidate(mobile, password)) return;
        dialog = LoadingDialog.create(this);
        dialog.show();
        userService.loginUser(user, new AbResponseListener<User>(new TypeReference<Result<User>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }

            @Override
            public void responseInfo(Result<User> data) {
                if (data.isSuccess()) {
                    dialog.dismiss();
                    ToastUtil.showMessage("登录成功!");
                    User user = data.getData();
                    PushAgent mPushAgent = PushAgent.getInstance(getBaseContext());
                    mPushAgent.enable();
                    try {
                        mPushAgent.addAlias(user.uid + "", "uid");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    saveUserInfo(user);
                    ReduceApplication.getInstance().finishActivity(LoginActivity.this);
                    forwardMainContent();
                }
            }
        });
    }

    /**
     * 忘记密码
     */
    public void forgetPassword() {
        Intent intent = new Intent(this, ResetPwdActivity.class);
        startActivity(intent);
    }

    /**
     * 跳转到主页
     */
    public void forwardMainContent() {
        Intent intent = new Intent(this, DoorActivity.class);
        startActivity(intent);
    }

    /**
     * 用户信息保存在本地
     *
     * @param user
     */
    public void saveUserInfo(User user) {
        UserPreference.saveUser(user);


    }

}
